package com.ym.b2b.commons.security;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class SessionFilter implements Filter {

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
                                                                                             ServletException {
        HttpServletRequest req = (HttpServletRequest) request;

        String path = req.getServletPath();
        if (path.indexOf("pmsapi") != -1) {

            Enumeration<String> enumeration = request.getParameterNames();
            while (enumeration.hasMoreElements()) {
                String parameterName = (String) enumeration.nextElement();
                if (parameterName.equals("businessCode")) {
                }
            }
            chain.doFilter(request, response);
            return;
        }
        Boolean grant = (Boolean) request.getAttribute("GRANT");

        if (grant != null && !grant) {
            System.out.println(grant);

            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<script>");
            out.println("window.location.href = '" + req.getContextPath() + "/login.jsp'");
            out.println("</script>");
            out.println("</html>");

        } else {
            chain.doFilter(request, response);
        }

    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }

}
